//工具包：绘制血量，判断矩形相交
#include "tools.h"
#include "variable.h"
#include <graphics.h>

bool rectIntersect(int x01, int y01, int x02, int y02,int x11, int y11, int x12, int y12){
    int zx = abs(x01 + x02 - x11 - x12);
    int x = abs(x01 - x02) + abs(x11 - x12);
    int zy = abs(y01 + y02 - y11 - y12);
    int y = abs(y01 - y02) + abs(y11 - y12);
    return  (zx <= x && zy <= y);
}


void drawBloodBar(int x, int y, int width, int height, int lineWidth, int boardColor, int emptyColor, int fillColor, float percent){
    LINESTYLE lineStyle;
    getlinestyle(&lineStyle);
    int lineColor = getlinecolor();
    int fileColor = getfillcolor();

    if (percent < 0) {
        percent = 0;
    }

    setlinecolor(BLUE);
    setlinestyle(PS_SOLID | PS_ENDCAP_ROUND, lineWidth);
    setfillcolor(emptyColor);
    fillrectangle(x, y, x + width, y + height);
    setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 0);
    setfillcolor(fillColor);
    setlinecolor(fillColor);
    if (percent > 0) {
        fillrectangle(x + 0.5 * lineWidth, y + lineWidth * 0.5, x + width * percent, y + height - 0.5 * lineWidth);
    }

    setlinecolor(lineColor);
    setfillcolor(fillColor);
    setlinestyle(&lineStyle);
}

int inArea(int mx, int my, int x, int y, int w, int h)
{
    if (mx >= x && my >= y && mx <= w && my <= h) return 1;
    return 0;
}



int button(int x, int y, const char* text, int d, COLORREF color1, COLORREF color2) {

    peekmessage(&ms,EX_MOUSE);
    // 文本宽和高
    int tw = textwidth(text);
    int th = textheight(text);

    // 文本坐标
    int tx = x - tw / 2;
    int ty = y - th / 2;

    // 矩形坐标和右下角坐标
    int rx = x - tw / 2 - d;
    int ry = y - th / 2 - d;
    int rx_w = x + tw / 2 + d;
    int ry_h = y + th / 2 + d;

    // 如果鼠标在按钮上就变色
    if (inArea(ms.x, ms.y, rx, ry, rx_w, ry_h)) {
        setfillcolor(color2);
    }
    else {
        setfillcolor(color1);
    }
    // 绘制矩形
    fillrectangle(rx, ry, rx_w, ry_h);

    // 绘制文字
    outtextxy(tx, ty, text);
    // 判断按钮是否按下
    if (ms.message == WM_LBUTTONDOWN && inArea(ms.x, ms.y, rx, ry, rx_w, ry_h)) {
        ms.message = 0;
        return 1;
    }
    return 0;
}